package com.example.yoga.controller;
/**
 * 公告
 */
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yoga.entity.Announcement;
import com.example.yoga.service.IAnnouncementService;
import com.example.yoga.util.JsonResult;
import com.example.yoga.util.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenkai
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementService announcementService;



    /**
     * 查询最新的10条公告
     */
    @RequestMapping(value = "/getNewAnno",method = RequestMethod.GET)
    public JsonResult<List> getNewAnno(){
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("publishTime");
        wrapper.last("limit 10");
        List<Announcement> list = announcementService.list(wrapper);
        if (list.size()>0){
            System.out.println(list);
            return new JsonResult<>(2000,list);
        }
        return new JsonResult<>(4000,"暂无数据！");
    }


    /**
     * 添加公告
     */
    @RequestMapping(value = "/addAnno",method = RequestMethod.POST)
    public JsonResult<Announcement> addAnno(@RequestBody Announcement announcement){
        //获取当前系统时间
        announcement.setPublishtime(new Date());
        boolean b = announcementService.save(announcement);
        if(b){
            return new JsonResult<>(2000,"发布成功");
        }
        return new JsonResult<>(4000,"发布失败！请重新发布");
    }

    /**
     * 修改公告
     */
    @RequestMapping(value="/updateAnno",method = RequestMethod.PUT)
    public JsonResult<Void> updateAnno(@RequestBody Announcement announcement){
        System.out.println(announcement.toString());
        //更新时间
        announcement.setPublishtime(new Date());
       //1.取出公告的id
        Integer annoid = announcement.getAnnoid();
        //根据id进行公告的更改
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.eq("annoId",annoid);
        boolean b = announcementService.update(announcement, wrapper);
        if (b){
            return new JsonResult<>(2000,"修改成功");
        }else{
            return new JsonResult<>(4000,"修改失败");
        }
    }

    /**
     * 删除公告
     * 从前台传的参数要与delAnno(Integer id)里面的这个id一一对应
     */
    @RequestMapping(value="/delAnno",method = RequestMethod.DELETE)
    public JsonResult<Void> delAnno(Integer id){
        System.out.println(id+"****************");
        if (id!=null){
            announcementService.removeById(id);
            return new JsonResult<>(2000,"删除成功");
        }else{
            return new JsonResult<>(4000,"删除失败");
        }

    }

    /**
     * 批量删除公告
     */
    @PostMapping("/delByIds")
    public JsonResult<Void> delByIds(@RequestBody List<Integer> ids){
        if (!ObjectUtils.isEmpty(ids)){
            announcementService.removeByIds(ids);
            return new JsonResult<>(2000,"删除成功");
        }
        return new JsonResult<>(4000,"请选择要删除的公告！");
    }

    /**
     * 查询所有公告（分页）
     */
    @RequestMapping(value = "/queryAll",method = RequestMethod.GET)
    public PageDataResult queryAll(Integer pageIndex,Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<Announcement> list = announcementService.list();
        PageInfo<Announcement> pageInfo = new PageInfo<>(list);
        PageDataResult pdr = new PageDataResult();
        pdr.setList(pageInfo.getList());
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        return pdr;
    }

    /**
     * 查询某个时间段内的公告
     */
    @RequestMapping("/queryByTime")
    public PageDataResult queryAnnoByTime(Integer pageIndex,Integer pageSize,String date1,String date2){
        //会在前端规定最早时间和最晚时间，格式不对查询不到
        //date1一定是最早时间，date2一定是最晚时间，否则查询不到记录
        //将字符串类型转为date类型（他会自动转换，不用转）
        PageHelper.startPage(pageIndex,pageSize);
        //两个时间区域之间的布告查询
        if (date1!=null&&date2!=null){
            QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
            wrapper.between("publishTime",date1,date2);
            List<Announcement> list = announcementService.list(wrapper);
            //return new JsonResult<List>(2000,list);
            PageInfo<Announcement> pageInfo = new PageInfo<>(list);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        //date1不为空，查询在这个时间之后的数据
        if (date1!=null&&date2==null){
            QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
            //ge是大于等于,le是小于等于
            wrapper.ge("publishTime",date1);
            List<Announcement> list = announcementService.list(wrapper);
            //return new JsonResult<List>(2000,list);
            PageInfo<Announcement> pageInfo = new PageInfo<>(list);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        //date2不为空，查询在这个时间之前的数据
        if (date1==null&&date2!=null){
            QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
            //ge是大于等于,Ie是小于等于
            wrapper.le("publishTime",date2);
            List<Announcement> list = announcementService.list(wrapper);
           // return new JsonResult<List>(2000,list);
            PageInfo<Announcement> pageInfo = new PageInfo<>(list);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        System.out.println(this.queryAll(pageIndex,pageSize));
        return this.queryAll(pageIndex,pageSize);
    }

}

